package thesis.filesharing.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import thesis.filesharing.bo.StudentBO;
import thesis.filesharing.common.exception.functional.InvalidArgumentException;
import thesis.filesharing.common.valueobjects.SearchStudentValueObject;
import thesis.filesharing.dao.StudentDAO;
import thesis.filesharing.model.Clazz;
import thesis.filesharing.model.Student;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Service
public class StudentBOImpl implements StudentBO {
	
	@Autowired
	private StudentDAO studentDAO;
	
	@Override
	public Student findStudentById(String studentId) {
		return studentDAO.findStudentById(studentId);
	}

	@Override
	public Student findStudentByEmail(String studentEmail) {
		return studentDAO.findStudentByEmail(studentEmail);
	}

	@Override
	public Student saveStudent(Student student) {
		return studentDAO.saveStudent(student);
	}

	@Override
	public List<Student> listStudents(int page, int pageSize) {
		if (page <= 0 || pageSize <= 0) {
			throw new InvalidArgumentException();
		}
		return studentDAO.listStudents(page, pageSize);
	}

	@Override
	public int countStudents() {
		return studentDAO.countStudents();
	}

	@Override
	public void deleteStudent(String key) {
		studentDAO.deleteStudent(KeyFactory.stringToKey(key));
	}

	@Override
	public Student findStudentByKey(Key studentKey) {
		return studentDAO.findStudentByKey(studentKey);
	}

	@Override
	public Student updateStudent(Student student) {
		return studentDAO.updateStudent(student);
	}

	@Override
	public boolean isEmailDuplicatedWithOrtherStudent(String studentEmail, Key studentKey) {
		return studentDAO.isEmailDuplicatedWithOrtherStudent(studentEmail, studentKey);
	}

	@Override
	public int countFoundStudents(SearchStudentValueObject searchObj) {
		return studentDAO.countFoundStudents(searchObj);
	}

	@Override
	public List<Student> searchStudent(SearchStudentValueObject searchObj, int page, int pageSize) {
		if (page <= 0 || pageSize <= 0) {
			throw new InvalidArgumentException();
		}
		return studentDAO.searchStudent(searchObj, page, pageSize);
	}

	@Override
	public List<Student> listStudentsInClass(String key, int page, int pageSize) {
		Key classKey = KeyFactory.stringToKey(key);
		return studentDAO.listStudentsInClass(classKey, page, pageSize);
	}

	@Override
	public int countStudentsInClass(String key) {
		Key classKey = KeyFactory.stringToKey(key);
		return studentDAO.countStudentsInClass(classKey);
	}

	@Override
	public List<Clazz> listClassesStudentRegistered(String studentId, Integer semester) {
		return studentDAO.listClassesStudentRegistered(studentId, semester);
	}

	@Override
	public boolean checkStudentRegisteredClass(String studentId, String classKey) {
		return studentDAO.checkStudentRegisteredClass(studentId, KeyFactory.stringToKey(classKey));
	}
}
